package com.cpi.cornpp.blockdistribution.service;

import com.cpi.cornpp.blockdistribution.converter.BlockDistributionConverter;
import com.cpi.cornpp.blockdistribution.dao.BlockDistributionDAO;
import com.cpi.cornpp.blockdistribution.dto.BlockDistributionDTO;
import com.cpi.cornpp.blockdistribution.exception.BlockDistributionException;
import com.cpi.cornpp.blockdistribution.view.BlockDistributionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;

public class BlockDistributionService {
	
	public BlockDistributionView getBlockDistribution(int purchaseOrderVersionId)throws BlockDistributionException{
		BlockDistributionView blockDistributionView = null;
		BlockDistributionDAO blockDistributionDAO = null;
		BlockDistributionDTO blockDistributionDTO = null;
		PurchaseOrderVersionService purchaseOrderVersionService = null;
		PurchaseOrderVersionView purchaseOrderVersionView = null;
		BlockDistributionConverter blockDistributionConverter = null;
		try{
			blockDistributionDAO = new BlockDistributionDAO();
			purchaseOrderVersionService = new PurchaseOrderVersionService();
			blockDistributionConverter = new BlockDistributionConverter();
			
			blockDistributionDTO = blockDistributionDAO.getBlockDistribution(purchaseOrderVersionId);
			purchaseOrderVersionView = purchaseOrderVersionService.getPurchaseOrderVersion(new Integer(purchaseOrderVersionId));
			blockDistributionView = blockDistributionConverter.convertDTOToView(purchaseOrderVersionView, blockDistributionDTO);
		}catch (BlockDistributionException blockDistributionException) {			
			throw blockDistributionException;
		}catch (Exception exception){
			BlockDistributionException blockDistributionException=null;
			exception.printStackTrace(System.out);
			blockDistributionException= new BlockDistributionException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw blockDistributionException;
		}
		return blockDistributionView;
	}
	
	public void saveBlockDistribution(BlockDistributionView blockDistributionView)throws BlockDistributionException{		
		BlockDistributionDTO blockDistributionDTO = null;
		BlockDistributionConverter blockDistributionConverter = null;
		BlockDistributionDAO blockDistributionDAO = null;
		try {
			blockDistributionConverter = new BlockDistributionConverter();
			blockDistributionDAO = new BlockDistributionDAO();
			blockDistributionDTO = blockDistributionConverter.convertViewToDTO(blockDistributionView);
			if( 0 == blockDistributionDTO.getBlockedDistributionId()){
				blockDistributionDAO.insert(blockDistributionDTO);
			}else{
				blockDistributionDAO.update(blockDistributionDTO);
			}
		}catch (BlockDistributionException blockDistributionException) {			
			throw blockDistributionException;
		}catch (Exception exception){
			BlockDistributionException blockDistributionException=null;
			exception.printStackTrace(System.out);
			blockDistributionException= new BlockDistributionException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw blockDistributionException;
		}
	}
	
	public void deleteBlockDistribution(int purchaseOrderVersionId)throws BlockDistributionException{
		BlockDistributionDAO tagDistributionDAO = null;
		try {
			tagDistributionDAO = new BlockDistributionDAO();
			tagDistributionDAO.delete(purchaseOrderVersionId);
		}catch (BlockDistributionException blockDistributionException) {			
			throw blockDistributionException;
		}catch (Exception exception){
			BlockDistributionException blockDistributionException=null;
			exception.printStackTrace(System.out);
			blockDistributionException= new BlockDistributionException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw blockDistributionException;
		}
	}
}